<cfoutput><CFQUERY NAME="getclient" DATASOURCE="#dsn#">
select *
from tblcustomers
where CustomerNum = <cfqueryparam cfsqltype="cf_sql_integer" value="#accountnumber#">

</CFQUERY>

<cfset today = now()>
<cfset mytime = timeformat(now(), "hh:mm:ss tt")>


<cflock timeout=20 scope="Session" type="Exclusive">

<cfset structdelete(SESSION, "prodid")>

</cflock>


<CFQUERY NAME="getprod" DATASOURCE="#dsn#">
select *
from tblproducts
where ProdID = '#ProdID#'
</CFQUERY>

<cfif #getprod.recordcount# lte 0>

<cflocation url="products.cfm?message=Item Not Found">



<cfabort>


<cfelse>


<cfif #getprod.Qty# is 0 or #qty# gt #getprod.Qty#>

<cflocation url="products.cfm?message=Out of Stock">


<cfelse>

<cfif #getprod.Tax# is 1>
<cfset caltax = #getprod.TaxAmount# / 100>
<cfset mytotal = #getprod.Price# * #qty#>

 <cfset mytax = #mytotal# * #caltax#>
 <cfelse>
  <cfset mytax = 0>
  <cfset mytotal = #getprod.Price# * #qty#>
</cfif>
 
<cfquery datasource="#dsn#" maxrows="1">
INSERT INTO tbltransactions(ReceiptNum, ProdID, Price, TransDate, TransType, ProductName, TaxAmount, Qty)
VALUES(#session.ReceiptNum#, 
'#ProdID#', 
#mytotal#, 
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#today#">,
'Purchase',
'#getprod.ProductName#',
#mytax#,
#Qty#
)
</cfquery>

<CFQUERY NAME="Recordset1" DATASOURCE="#dsn#">
select sum(Price) as total, sum(TaxAmount) as taxtotal
from tbltransactions
where ReceiptNum = #session.ReceiptNum#
</CFQUERY>
<!---update main invoice with total amount-------------------->

<cfif #Recordset1.taxtotal# lte 0>

<cfset Recordset1.taxtotal = 0>

<cfelse>

<cfset Recordset1.taxtotal = 0>

</cfif>

<cfset mytaxtotal = #Recordset1.total# + #Recordset1.taxtotal#>

<cfquery datasource="#dsn#">
update tblorders
set Amount = #mytaxtotal#
where ReceiptNum = #session.ReceiptNum# 
</cfquery>

<!---update inventory..............................................--->

<cfif #getprod.Qty# gte 1>
<cfset myqty = #getprod.Qty# - #Qty#>
<cfelse>
<cfset myqty = 0>

</cfif>

<cfquery datasource="#dsn#">
update tblproducts
set Qty = #myqty#
where ProdID = '#ProdID#'
</cfquery>




<cfquery datasource="#dsn#">
INSERT INTO tblinventory(ReceiptNum, ProdID, Price, DateIn, DODAAC, Username, Terminal, Qty, TransType, Cost)
VALUES(#session.ReceiptNum#, 
'#ProdID#', 
#getprod.Price#, 
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#today#">,
<cfif isdefined("session.DODAAC")>#session.DODAAC#<cfelse>null</cfif>,
'#session.username#',
#session.terminal#,
#getprod.Qty#,
'SALE',
<cfif #getprod.Cost# lte 0>0<cfelse>#getprod.Cost#</cfif>)
</cfquery>








<CFQUERY DATASOURCE="#dsn#">
INSERT INTO tbltranslog (Code, ReceiptNum, TransDate, TransTime, username, ProdID, Amount)
VALUES (
'A',
#session.ReceiptNum#,
<cfqueryparam cfsqltype="cf_sql_date" value="#today#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#mytime#">,
'#session.username#',
'#ProdID#',
#mytaxtotal#

)
</CFQUERY>






<cflocation url="products.cfm">
</cfif>

</cfif>

</cfoutput>
